package com.study.chapter05.operator.sink;

import com.study.entity.Event;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;
import org.apache.kafka.clients.consumer.ConsumerConfig;

import java.util.Properties;

/**
 * @Description:
 * @Author: LiuQun
 * @Date: 2022/7/30 16:18
 */
public class Sink2KafkaTest2 {

    public static void main(String[] args) throws Exception {
        //环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        Properties properties = new Properties();
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.30.100:9092");

        //创建kafka的消费者，从topic为clicks中消费数据
        FlinkKafkaConsumer<String> kafkaConsumer = new FlinkKafkaConsumer<String>(
                "clicks",
                new SimpleStringSchema(),
                properties
        );
        //1.从kafka中读取数据
        DataStreamSource<String> kafkaStream = env.addSource(kafkaConsumer);

        //2.用flink对数据进行转换
        SingleOutputStreamOperator<String> result = kafkaStream.map(new MapFunction<String, String>() {

            @Override
            public String map(String value) throws Exception {
                String[] fieldArr = value.split(",");
                return new Event(fieldArr[0].trim(), fieldArr[1].trim(), Long.valueOf(fieldArr[2].trim()))
                        .toString();
            }
        });

        //3.将数据写入kafka中
        //创建kafka的生产者，往topic为events中写入数据
        FlinkKafkaProducer<String> kafkaProducer = new FlinkKafkaProducer<>("192.168.30.100:9092", "events", new SimpleStringSchema());
        //将数据写入kafka
        result.addSink(kafkaProducer);

        //在服务器中分别启动kafka的生产者和消费者，生产者中进行生产数据，消费这种进行消费数据，看效果
        //./kafka-console-producer --broker-list 192.168.30.100:9092 --topic clicks
        //./kafka-console-consumer --bootstrap-server 192.168.30.100:9092 --topic events

        env.execute();
    }
}
